using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using SPVisualDev.VSAddin.Dialogs;

namespace SPVisualDev.VSAddin
{
    public class NewSPASPXFileWizard: IWizard
    {
        private DTE2 m_DTE;

        public NewSPASPXFileWizard()
        {
        }

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {
                //System.Diagnostics.Debugger.Break();

                m_DTE = (DTE2)automationObject;

                string safe_item_name = replacementsDictionary["$rootname$"];
                if (safe_item_name.IndexOf(".") != -1)
                    safe_item_name = safe_item_name.Split('.')[0];

                safe_item_name = safe_item_name.Replace(".", "_");

                replacementsDictionary.Add("$customnamespace$", "DefaultNamespace");

                if (m_DTE.SelectedItems.Count > 0)
                {
                    Project proj = m_DTE.SelectedItems.Item(1).Project;
                    if (proj == null)
                    {
                        proj = m_DTE.SelectedItems.Item(1).ProjectItem.ContainingProject;
                    }

                    if (Common.CheckIfSpecialProject(proj))
                    {
                        string default_ns = proj.Properties.Item("DefaultNamespace").Value.ToString();
                        string template_name = System.IO.Path.GetFileName(customParams.GetValue(0).ToString());
                        string custom_ns = string.Empty;

                        if (template_name == "SharePointMinimalApplicationPage.vstemplate" ||
                            template_name == "SharePointMiinimalPageWithLeftNav.vstemplate")
                        {
                            custom_ns = Common.GetProjectGlobalConfigKey(proj, "ASPXPagesNSsuffix");
                            if (string.IsNullOrEmpty(custom_ns))
                                custom_ns = "ApplicationPages";
                        }
                        else if (template_name == "SharePointUserControl.vstemplate")
                        {
                            custom_ns = Common.GetProjectGlobalConfigKey(proj, "WebCustomConstrolNSsuffix");
                            if (string.IsNullOrEmpty(custom_ns))
                                custom_ns = "WebControls";
                        }
                        else if (template_name == "SharePointMinimalMasterPage.vstemplate")
                        {
                            custom_ns = Common.GetProjectGlobalConfigKey(proj, "MasterPagesNSsuffix");
                            if (string.IsNullOrEmpty(custom_ns))
                                custom_ns = "MasterPages";
                        }

                        string ns = default_ns + "." + custom_ns;
                        replacementsDictionary["$customnamespace$"] = ns;

                        string asm_name = Common.GetTargetBuildAssemblyName(proj, false);
                        if (asm_name.ToLower().IndexOf("version=") != -1)
                            replacementsDictionary.Add("$inherits$", ns + "." + safe_item_name + ", " + asm_name);
                        else
                            replacementsDictionary.Add("$inherits$", ns + "." + safe_item_name);
                    }
                    else
                    {
                        replacementsDictionary.Add("$inherits$", replacementsDictionary["$rootnamespace$"]);
                    }
                }
                else
                {
                    replacementsDictionary.Add("$inherits$", replacementsDictionary["$rootnamespace$"]);
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
